package com.net.xpay.common.manager.impl.partner;

import com.google.common.base.Preconditions;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.xpay.common.dao.partner.PartnerCombinationSettleRecordMapper;
import com.net.xpay.common.domain.partner.PartnerCombinationSettleRecord;
import com.net.xpay.common.enums.partner.PartnerCombinationSettleRecordType;
import com.net.xpay.common.manager.partner.PartnerCombinationSettleRecordManager;
import com.net.common.manager.AbstractManager;
import com.net.xpay.core.model.PartnerCombinationSettleRecordAggSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Component
public class PartnerCombinationSettleRecordManagerImpl extends AbstractManager<PartnerCombinationSettleRecord> implements PartnerCombinationSettleRecordManager{

    @Autowired
    private PartnerCombinationSettleRecordMapper mapper;


    @Override
    protected BasicMapper<Long, PartnerCombinationSettleRecord> getMapper() {
        return mapper;
    }

    @Override
    public PartnerCombinationSettleRecord findByDayAndUserId(Long userId, LocalDate day, PartnerCombinationSettleRecordType type) {
        Preconditions.checkArgument(userId != null && day != null && type != null);
        PartnerCombinationSettleRecord condition = new PartnerCombinationSettleRecord();
        condition.setUserId(userId);
        condition.setDay(day);
        condition.setType(type);
        return getByCondition(condition);
    }

    @Override
    public int updateSettleInfo(Long partnerId, PartnerCombinationSettleRecordType type, LocalDate startDay, LocalDate endDay, LocalDateTime settleTime) {
        Preconditions.checkArgument(partnerId != null && type != null && startDay != null && endDay != null && settleTime != null);
        return mapper.updateSettleInfo(partnerId, type, startDay , endDay, settleTime);
    }

    @Override
    public List<PartnerCombinationSettleRecord> aggSearch(PartnerCombinationSettleRecordAggSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        return nonEmptyList(mapper.aggSearch(searchModel, page));
    }

    @Override
    public List<PartnerCombinationSettleRecord> aggList(PartnerCombinationSettleRecordAggSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.aggList(searchModel));
    }

    @Override
    public long aggSearchCount(PartnerCombinationSettleRecordAggSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.aggSearchCount(searchModel);
    }

    @Override
    public List<PartnerCombinationSettleRecord> partnerAggSearch(PartnerCombinationSettleRecordAggSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        return nonEmptyList(mapper.partnerAggSearch(searchModel, page));
    }

    @Override
    public long partnerAggSearchCount(PartnerCombinationSettleRecordAggSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.partnerAggSearchCount(searchModel);
    }

    @Override
    public List<PartnerCombinationSettleRecord> aggSumUserMonthData(Long userId, LocalDate endDay) {
        Preconditions.checkArgument(userId != null && endDay != null);
        return mapper.aggSumUserMonthData(userId, endDay);
    }

    @Override
    @Transactional(transactionManager = "xpayCommonTransactionManager", rollbackFor = Exception.class)
    public void bothSave(PartnerCombinationSettleRecord record, PartnerCombinationSettleRecord fatherRecord) {
        save(record);
        save(fatherRecord);
    }
}
